//
//  TradeChangeView.m
//  bitcoin
//
//  Created by  旺链 on 2018/5/31.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "TradeChangeView.h"

@interface TradeChangeView()
{
    //可支付数
    NSString *fundPay;
    //可交易数
    NSString *fundTrade;
    
//    //支付币数量精度
//    NSString *mainCurrentUnitPrecision;
    //数量精度
    NSString *tradeCurrencyUnitPrecision;
    
    //价格精度
    NSString *mainCurrencyDecimalPrecision;
//    //交易币价格精度
//    NSString *tradeCurrencyDecimalPrecision;
}
@end

@implementation TradeChangeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//- (instancetype)initWithCoder:(NSCoder *)aDecoder
//{
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        
//        self = [[[NSBundle mainBundle]loadNibNamed:@"TradeChangeView" owner:self options:nil]firstObject];
//    }
//    return self;
//}
//
- (void)awakeFromNib
{
    [super awakeFromNib];
//    [self addSubview:self.slider];
    [self addSubview:self.zzslider];
    fundPay = @"0";
    fundTrade = @"0";
    self.isNewRelation = NO;
//    [self.sli setThumbImage:[UIImage imageWithColor:[UIColor redColor] withRect:CGRectMake(0, 0, 10, 10)] forState:UIControlStateNormal];
}
- (ZZslider *)zzslider
{
    if (!_zzslider) {
        _zzslider = [[ZZslider alloc]initWithFrame:CGRectMake(5, self.leftMoneyLb.mm_bottom +10, ScreenWidth/2-30-10, 55)];
    
        [_zzslider setThumbImage:[UIImage imageNamed:@"green-icon"] forState:UIControlStateNormal];
        
        _zzslider.minimumTrackTintColor = [UIColor colorWithHexString:@"#2CB25A"];
        [_zzslider addTarget:self action:@selector(valueChangeAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _zzslider;
}
//- (CCHStepSizeSlider *)slider
//{
//    if (!_slider) {
//        _slider = [[CCHStepSizeSlider alloc] initWithFrame:CGRectMake(15, self.leftMoneyLb.mm_bottom, ScreenWidth/2-30, 55)];
//        _slider.backgroundColor = [UIColor clearColor];
//        _slider.type = CCHStepSizeSliderTypeNormal;
//        //    slider2.titleArray = @[@"",@"你好",@"好吗",@"好的"];
//        _slider.lineWidth = 4;
//        //    slider2.titleOffset = 23;
//        //    slider2.titleColor = [UIColor blueColor];
//        _slider.thumbColor = [UIColor colorWithHexString:@"#2CB25A"];
//        _slider.stepColor = [UIColor colorWithHexString:@"#45485A"];
//        _slider.minTrackColor = [UIColor colorWithHexString:@"#2CB25A"];
//        _slider.lineColor = [UIColor colorWithHexString:@"#45485A"];
////        _slider.index = 0;
////        _slider.stepWidth = 14;
//        _slider.margin = 0;
//       // _slider.sliderOffset = -8;
////        _slider.stepTouchRate = 5;
//        _slider.thumbSize = CGSizeMake(14, 14);
//        _slider.thumbTouchRate = 1;
//        //    slider2.numberOfStep = 3;
//        _slider.continuous = NO;
//        [_slider addTarget:self action:@selector(valueChangeAction:) forControlEvents:UIControlEventValueChanged];
//
//    }
//    return _slider;
//}
- (IBAction)addPrice:(id)sender {
    //    NSString *str = self.inputPriceTf.text;
    //
    //    NSArray *arr = [str componentsSeparatedByString:@"."];
    //
    //    NSString *s = [arr lastObject];
    //    NSLog(@"%lu,%f",(unsigned long)s.length,powf(10, s.length));
    
    NSInteger Precision = mainCurrencyDecimalPrecision.integerValue;
    
    double f = self.inputPriceTf.text.doubleValue;
    
    double i = 1.0/powf(10, Precision);
    f += i;
    
    NSString *x = [NSString stringWithFormat:@"%.8f",f];
    
    self.inputPrice = [self editStr:x withPrecision:Precision];
}
- (IBAction)subPrice:(id)sender {

    NSInteger Precision = mainCurrencyDecimalPrecision.integerValue;
    
    double f = self.inputPriceTf.text.doubleValue;
    
    double i = 1.0/powf(10, Precision);
    f -= i;
    
    NSString *x = [NSString stringWithFormat:@"%.8f",f];
    
    self.inputPrice = [self editStr:x withPrecision:Precision];
}
- (NSString*)editStr:(NSString*)x withPrecision:(NSInteger)Precision
{
    NSString *editedStr;
    NSInteger loca = [x rangeOfString:@"."].location;
    if (loca+Precision+1 <x.length&&loca>0) {
        editedStr = [x substringToIndex:loca+Precision+1];
    }else{
        editedStr = x;
    }
    return editedStr;
}

- (void)setPrecisionDic:(NSDictionary *)precisionDic
{
    _precisionDic = precisionDic;
    
//    mainCurrentUnitPrecision = precisionDic[@"mainCurrentUnitPrecision"];
    mainCurrencyDecimalPrecision = precisionDic[@"mainCurrencyDecimalPrecision"];
    
    tradeCurrencyUnitPrecision = precisionDic[@"tradeCurrencyUnitPrecision"];
//    tradeCurrencyDecimalPrecision = precisionDic[@"tradeCurrencyDecimalPrecision"];
}
- (void)setSellFirstPrice:(NSString *)sellFirstPrice
{
    _sellFirstPrice = sellFirstPrice;
    if (!self.isSell) {
        
        if (self.isNewRelation) {
            
            self.inputPrice = sellFirstPrice;
        }else{
            
            if (!self.inputPrice) {
                self.inputPrice = sellFirstPrice;
            }
        }
    }
    //重置
    self.isNewRelation = NO;
}
- (void)setBuyFirstPrice:(NSString *)buyFirstPrice
{
    _buyFirstPrice = buyFirstPrice;
    if (self.isSell) {
        
        if (self.isNewRelation) {
            
            self.inputPrice = buyFirstPrice;
        }else{
            
            if (!self.inputPrice) {
                self.inputPrice = buyFirstPrice;
            }
        }
        
    }
    //重置
    self.isNewRelation = NO;
}
- (void)setInputPrice:(NSString *)inputPrice
{
    //价格变化
    _inputPrice = inputPrice.length>0?inputPrice:@"0";
    self.inputPriceTf.text = inputPrice.length>0?inputPrice:@"0";
    
    NSDictionary *para = @{@"currencyId":self.bId,@"toCurrency":self.bName,@"amount":inputPrice.length>0?inputPrice:@"0"};
    [LSRequestManager postRequestWithURL:MARKET(@"currencyConversion") params:para success:^(NSDictionary *responseData) {
        
        NSString *num = @"0";
        HandleResponse(
        
                       num = responseData[@"data"][@"legalQuantity"];
        
                       )}
     self.conversionLb.text = [NSString stringWithFormat:@"≈ %@ %@",num,self.bName];
     } failure:^(NSError *error) {
         
     }];
    if (!self.isSell&&!self.isMarkertPrice) {
        
        double max = 0;
        if (_inputPrice.doubleValue!=0) {
            max= fundPay.doubleValue/_inputPrice.doubleValue;
        }
        self.maxNumMoneyLb.text = [NSString stringWithFormat:@"%.4f %@",max,self.tradeCurrency];
        
        NSString *x =[NSString stringWithFormat:@"%.8f",self.zzslider.value*(max)];
        self.countTf.text = [self editStr:x withPrecision:tradeCurrencyUnitPrecision.integerValue];
    }
}

- (void)setIsMarkertPrice:(BOOL)isMarkertPrice
{
    _isMarkertPrice = isMarkertPrice;
    if (_isMarkertPrice) {
        self.marketPriceView.hidden = NO;
        self.tradeMoneyLb.hidden = YES;
        
        self.countTf.placeholder = @"交易额";
    }else{
        self.marketPriceView.hidden = YES;
        self.tradeMoneyLb.hidden = NO;
        
        self.countTf.placeholder = @"数量";
    }
    
    [self changeState];
}

-(void)setAssetsData:(NSDictionary *)assetsData
{
    if ([assetsData[@"currencyFundUsable"]isKindOfClass:[NSNull class]]) {
        
        return;
    }
    BOOL isChange = NO;
    if (![fundPay isEqualToString:assetsData[@"currencyFundUsable"]]&&![fundTrade isEqualToString:assetsData[@"currencyFundTrade"]]) {
        //资产变化
        isChange = YES;
    }
    if (assetsData&&![assetsData isKindOfClass:[NSNull class]]) {
        _assetsData = assetsData;
        fundPay = assetsData[@"currencyFundUsable"];
        fundTrade = assetsData[@"currencyFundTrade"];
    }else{
        fundPay = @"0";
        fundTrade = @"0";
    }
    
    if (isChange) {
        [self changeState];
        
        if (self.maxNumMoneyLb.text.floatValue <= 0) {
            
            double max = 0;
            if (self.inputPrice.doubleValue!=0) {
                max= fundPay.doubleValue/self.inputPrice.doubleValue;
            }
            self.maxNumMoneyLb.text = [NSString stringWithFormat:@"%.4f %@",max,self.tradeCurrency];

            NSString *x =[NSString stringWithFormat:@"%.8f",self.zzslider.value*(max)];
            self.countTf.text = [self editStr:x withPrecision:tradeCurrencyUnitPrecision.integerValue];
        }
    }
    
}

- (void)setIsSell:(BOOL)isSell
{
    _isSell = isSell;
    
    //清空数量
    self.zzslider.value = 0;
    NSInteger Precision = tradeCurrencyUnitPrecision.integerValue;
    NSString *x =[NSString stringWithFormat:@"%.8f",0.0];
    self.countTf.text = [self editStr:x withPrecision:Precision];
    
    if (_isSell) {
        //卖 设置买一价格
        if (self.buyFirstPrice.length) {
            self.inputPrice = self.buyFirstPrice;
        }
    }else{
        //买 设置卖一价格
        if (self.sellFirstPrice.length) {
            self.inputPrice = self.sellFirstPrice;
        }
    }
    [self changeState];
    
}
- (void)setTradeCurrency:(NSString *)tradeCurrency
{
    _tradeCurrency = tradeCurrency;
}
- (void)setPayCurrency:(NSString *)payCurrency
{
    _payCurrency = payCurrency;
    [self changeState];
}
- (void)setCurrencyRelation:(NSString *)currencyRelation
{
    //设置单位
    _currencyRelation = currencyRelation;
    NSArray *arr = [currencyRelation componentsSeparatedByString:@"/"];
    
    NSString *trade = arr[0];
    NSString *pay = arr[1];
    if ([trade isEqualToString:self.tradeCurrency]&&[pay isEqualToString:self.payCurrency]) {
        //没有切换交易对
        self.isNewRelation = NO;
    }else{
        self.isNewRelation = YES;
    }
    
    self.tradeCurrency = arr[0];
    self.payCurrency = arr[1];
}
- (void)changeState
{
    //改变交易币，支付币
    if (!self.isSell) {
        //买入
        if (self.isMarkertPrice) {
            //市价
            self.numB.text = self.payCurrency;
            
            self.maxNumMoneyLb.text = [NSString stringWithFormat:@"%@ %@",fundPay,self.payCurrency];
        }else{
            //限价
            self.numB.text = self.tradeCurrency;
            
            //self.maxNumMoneyLb.text = [NSString stringWithFormat:@"%@ %@",fundTrade,self.tradeCurrency];
        }
        
        self.leftMoneyLb.text = [NSString stringWithFormat:@"可用 %@ %@",fundPay,self.payCurrency];
    }else{
        self.numB.text = self.tradeCurrency;
        self.maxNumMoneyLb.text = [NSString stringWithFormat:@"%@ %@",fundTrade,self.tradeCurrency];
        
        self.leftMoneyLb.text = [NSString stringWithFormat:@"可用 %@ %@",fundTrade,self.tradeCurrency];
    }
}
- (void)valueChangeAction:(UISlider *)sender {
    
    NSString *numStr;
    NSInteger Precision = tradeCurrencyUnitPrecision.integerValue;
    if (!self.isMarkertPrice) {
        //限价
        //numStr = fundTrade;
        numStr = self.maxNumMoneyLb.text;
//        Precision = tradeCurrencyUnitPrecision.integerValue;
    }else{
        if (!self.isSell) {
            //买入
            numStr = fundPay;
//            Precision = mainCurrentUnitPrecision.integerValue;
        }else{
            numStr = fundTrade;
//            Precision = tradeCurrencyUnitPrecision.integerValue;
        }
        
    }
    
    NSString *x =[NSString stringWithFormat:@"%.8f",sender.value*(numStr.floatValue)];
    self.countTf.text = [self editStr:x withPrecision:Precision];
    
    if (!self.isMarkertPrice) {
    
        self.tradeMoneyLb.text = [NSString stringWithFormat:@"交易额 %f",self.countTf.text.floatValue*self.inputPrice.floatValue];
    }
}
@end
